package com.jcwx.game.client;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import com.alibaba.fastjson.JSONObject;

public class MinaTimeClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		NioSocketConnector connector = new NioSocketConnector();
		connector.getFilterChain().addLast("logger", new LoggingFilter());
		connector.getFilterChain().addLast(
				"codec",
				new ProtocolCodecFilter(new TextLineCodecFactory(Charset
						.forName("UTF-8")))); // 设置编码过滤器
		connector.setConnectTimeout(30);
		connector.setHandler(new TimeClientHandler());// 设置事件处理器
		ConnectFuture cf = connector.connect(new InetSocketAddress("127.0.0.1",
				8888));// 建立连接
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("body",
				"[139135, , 1400913814961, 39cb0d81756dbc23c3d95204e9949d31, 1, 0, wonder, 153, Windows]");
		map.put("head", 3);
		System.out.println(JSONObject.toJSON(map));
		cf.awaitUninterruptibly();// 等待连接创建完成
		cf.getSession().write("{body=[703337, 570, 2, 2010101], head=21}");// 发送消息
		// cf.getSession().write("quit");//发送消息
		cf.getSession().getCloseFuture().awaitUninterruptibly();// 等待连接断开
		connector.dispose();
	}

}
